var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 1; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    };
Object.defineProperty(i, "__esModule", {value: !0});
var s,
    c,
    l = e("AssetsMap"),
    d = e("BundleNames"),
    u = e("GlobalParam"),
    p = e("Util"),
    h = e("GameData"),
    f = e("nodePoolManager"),
    _ = e("SkinLevelData"),
    y = e("blood_prefab");
((c = s || (s = {}))[(c.none = 0)] = "none"),
    (c[(c.openMouth = 1)] = "openMouth"),
    (c[(c.shutUp = 2)] = "shutUp"),
    (c[(c.smile = 3)] = "smile");
var m = cc._decorator,
    g = m.ccclass,
    v =
        (m.property,
        (function (e) {
            function t() {
                var t = (null !== e && e.apply(this, arguments)) || this;
                return (
                    (t.snakeControl = null),
                    (t.snakeSkinNodes = []),
                    (t.skin_name = ""),
                    (t.isPlayerEatAudio = !1),
                    (t.isPlayerEatAudio_dt = 0),
                    (t.snake_head = null),
                    (t.snake_tail = null),
                    (t.snake_shield = null),
                    (t.is_show_snake_shield = !1),
                    (t.snake_size_left = 0),
                    (t.snake_size_right = 0),
                    (t.snake_size_top = 0),
                    (t.snake_size_bottom = 0),
                    (t.snakeHeadAni = s.none),
                    (t._addSpeedNode = []),
                    (t._addSpeedDance = 0),
                    t
                );
            }
            return (
                a(t, e),
                (t.prototype.init = function (e) {
                    (this.snakeControl = e),
                        (this.skin_name = _.SkinLevelData.instance.skinList[this.snakeControl.skin_index].name);
                    for (var t = 0; t < this.snakeSkinNodes.length; t++)
                        cc.isValid(this.snakeSkinNodes[t]) && f.default.instance.delBodyNode(this.snakeSkinNodes[t]),
                            (this.snakeSkinNodes[t] = null);
                }),
                (t.prototype.setIgnore = function () {}),
                (t.prototype.__update = function (e) {
                    if (h.default.instance.gameState == h.GameState.play && !this.snakeControl.isDie) {
                        if (this.snake_head)
                            (this.snake_head.scale = this.snakeControl.snake_scale),
                                (this.snake_head.position = this.snakeControl.head_pos),
                                (this.snake_head.getChildByName("image").angle = this.snakeControl.snake_angle - 90);
                        else {
                            (this.snake_head = f.default.instance.getHeadNode()),
                                (this.snake_head.parent = this.node),
                                (this.snake_head.zIndex = cc.macro.MAX_ZINDEX),
                                (this.snake_head.x = 1e4);
                            var t = game.BundleManager.instance
                                .getBundle(d.default.Game)
                                .get(
                                    l.default.GameBundle.textures.assetsList[this.skin_name + "_head_0"],
                                    cc.SpriteFrame
                                );
                            (this.snake_head
                                .getChildByName("image")
                                .getChildByName("sprite")
                                .getComponent(cc.Sprite).spriteFrame = t),
                                this.refreshDressUp(),
                                this.snakeControl.mainplayer &&
                                    f.default.instance.gameModelInstance.gameModel == h.GameModel.eatChicken &&
                                    this.attackRangeBlue();
                        }
                        if (!this.snake_tail) {
                            (this.snake_tail = f.default.instance.getTailNode()),
                                (this.snake_tail.parent = this.node),
                                (this.snake_tail.zIndex = -cc.macro.MAX_ZINDEX);
                            var o = this.snake_tail.getChildByName("sprite"),
                                n = game.BundleManager.instance
                                    .getBundle(d.default.Game)
                                    .get(
                                        l.default.GameBundle.textures.assetsList[this.skin_name + "_tail"],
                                        cc.SpriteFrame
                                    );
                            o.getComponent(cc.Sprite).spriteFrame = n;
                        }
                        if (this.snakeControl.snake_body.length > 0) {
                            var i = this.snakeControl.snake_body[this.snakeControl.snake_body.length - 1];
                            (this.snake_tail.angle = i.angle + 90),
                                (this.snake_tail.x = this.snakeControl.tail_pos.x),
                                (this.snake_tail.y = this.snakeControl.tail_pos.y),
                                (this.snake_tail.scale = this.snakeControl.snake_scale);
                        }
                        for (
                            var a = this.snakeControl.snakeUtil.currentDistance, r = 0;
                            r < this.snakeControl.snake_body_length;
                            r++
                        ) {
                            var s = r * a + a - 1,
                                c = this.snakeControl.snake_body[s];
                            if (this.isShow(c)) {
                                if (cc.isValid(this.snakeSkinNodes[r])) {
                                    var u = this.snakeSkinNodes[r];
                                    (u.x = c.x), (u.y = c.y), (u.scale = this.snakeControl.snake_scale);
                                } else {
                                    var p = f.default.instance.getBodyNode();
                                    (p.parent = this.node),
                                        (p.zIndex = 0 - r),
                                        (this.snakeSkinNodes[r] = p),
                                        (p.x = c.x),
                                        (p.y = c.y),
                                        (p.scale = this.snakeControl.snake_scale);
                                    var _ = r % 2 == 0 ? 1 : 2,
                                        y = this.skin_name + "_body_" + _,
                                        m = game.BundleManager.instance
                                            .getBundle(d.default.Game)
                                            .get(l.default.GameBundle.textures.assetsList[y], cc.SpriteFrame);
                                    p.getComponent(cc.Sprite).spriteFrame = m;
                                }
                            } else
                                cc.isValid(this.snakeSkinNodes[r]) &&
                                    (f.default.instance.delBodyNode(this.snakeSkinNodes[r]),
                                    (this.snakeSkinNodes[r] = null));
                            0 == r
                                ? ((this.snake_size_left = c.x),
                                  (this.snake_size_right = c.x),
                                  (this.snake_size_top = c.y),
                                  (this.snake_size_bottom = c.y))
                                : (c.x < this.snake_size_left && (this.snake_size_left = c.x),
                                  c.x > this.snake_size_right && (this.snake_size_right = c.x),
                                  c.y < this.snake_size_bottom && (this.snake_size_bottom = c.y),
                                  c.y > this.snake_size_top && (this.snake_size_top = c.y));
                        }
                        if (
                            (this.showAddSpeedAni(),
                            this.refreshShieldPos(),
                            (this.isPlayerEatAudio_dt += e),
                            this.isPlayerEatAudio_dt > 0.1 && ((this.isPlayerEatAudio_dt = 0), this.isPlayerEatAudio))
                        ) {
                            this.isPlayerEatAudio = !1;
                            var g = game.BundleManager.instance
                                .getBundle(d.default.Game)
                                .get(l.default.GameBundle.sounds.assetsList.eating, cc.AudioClip);
                            game.AudioManager.playEffect(g);
                        }
                    }
                }),
                (t.prototype.attackRangeBlue = function () {
                    var e = game.BundleManager.instance
                            .getBundle(d.default.Game)
                            .get(l.default.GameBundle.textures.assetsList.AttackRange_blue_size, cc.SpriteFrame),
                        t = game.BundleManager.instance
                            .getBundle(d.default.Game)
                            .get(l.default.GameBundle.textures.assetsList.AttackRange_blue_icon, cc.SpriteFrame),
                        o = this.snake_head.getChildByName("image").getChildByName("AttackRange");
                    o.getChildByName("bg").getComponent(cc.Sprite).spriteFrame = e;
                    for (var n = 1; n < 5; n++)
                        o.getChildByName("direction" + n).getComponent(cc.Sprite).spriteFrame = t;
                }),
                (t.prototype.isShow = function (e) {
                    var t = f.default.instance.gameModelInstance._gameCameraPos,
                        o = h.default.instance.size;
                    return (
                        e.x >= t.x - o.width / 2 &&
                        e.x <= t.x + o.width / 2 &&
                        e.y >= t.y - o.height / 2 &&
                        e.y <= t.y + o.height / 2
                    );
                }),
                (t.prototype.onSnakeDie = function () {
                    this.snake_head && (this.snake_head.x = 1e4),
                        this.snake_tail && (f.default.instance.delTailNode(this.snake_tail), (this.snake_tail = null)),
                        h.default.instance.GameModel != h.GameModel.eatChicken || this.snakeControl.mainplayer
                            ? h.default.instance.GameModel == h.GameModel.TimeLimit
                                ? this.snakeDieAddBlast()
                                : h.default.instance.GameModel == h.GameModel.endless &&
                                  (this.snakeControl.mainplayer || this.snakeDieAddBlast())
                            : this.snakeDieAddBlast();
                }),
                (t.prototype.snakeDieAddBlast = function () {
                    for (var e = [], t = 0; t < this.snakeSkinNodes.length; t++)
                        if (cc.isValid(this.snakeSkinNodes[t])) {
                            var o = this.snakeSkinNodes[t];
                            t % 2 == 0 && e.push(cc.v2(o.x, o.y)),
                                f.default.instance.delBodyNode(o),
                                (this.snakeSkinNodes[t] = null);
                        }
                    f.default.instance.gameModelInstance._layer_blast.ShowBlast(e);
                }),
                (t.prototype.onEnable = function () {
                    this.schedule(this.refreshSnakeHeadAni, 0.2);
                }),
                (t.prototype.onDisable = function () {
                    this.unschedule(this.refreshSnakeHeadAni);
                }),
                (t.prototype.refreshSnakeHeadAni = function () {
                    if (this.snakeHeadAni != s.none) {
                        var e;
                        e =
                            this.snakeHeadAni == s.smile
                                ? this.skin_name + "_head_0"
                                : this.skin_name + "_head_" + this.snakeHeadAni;
                        var t = game.BundleManager.instance
                            .getBundle(d.default.Game)
                            .get(l.default.GameBundle.textures.assetsList[e], cc.SpriteFrame);
                        (this.snake_head
                            .getChildByName("image")
                            .getChildByName("sprite")
                            .getComponent(cc.Sprite).spriteFrame = t),
                            this.snakeHeadAni == s.openMouth
                                ? (this.snakeHeadAni = s.shutUp)
                                : this.snakeHeadAni == s.shutUp
                                ? (this.snakeHeadAni = s.smile)
                                : this.snakeHeadAni == s.smile && (this.snakeHeadAni = s.none);
                    }
                }),
                (t.prototype.eatFood = function (e) {
                    if (
                        (h.default.instance.isStoreHose &&
                            e.foodType === h.FoodType.StoreFood &&
                            this.show_blood(!1, !0),
                        (this.snakeHeadAni = s.openMouth),
                        e.node)
                    ) {
                        var t = f.default.instance.gameModelInstance._gameCameraPos,
                            o = h.default.instance.size;
                        if (
                            e.x >= t.x - o.width / 2 &&
                            e.x <= t.x + o.width / 2 &&
                            e.y >= t.y - o.height / 2 &&
                            e.y <= t.y + o.height / 2
                        ) {
                            var n = this.snake_head.getChildByName("image").getChildByName("target_eat"),
                                i = e.node.parent.convertToWorldSpaceAR(e.node.position),
                                a = n.convertToNodeSpaceAR(i);
                            (e.node.parent = n), (e.node.position = a);
                            var r = this;
                            cc.tween(e.node)
                                .to(0.3, {position: cc.v3(0, 0, 0), scale: 0})
                                .call(function () {
                                    r.snakeControl.mainplayer &&
                                        0 == e.isRecovery &&
                                        e.foodType == h.FoodType.DieFood &&
                                        (r.isPlayerEatAudio = !0),
                                        f.default.instance.delFoodNode(e.node),
                                        (e.node = null);
                                })
                                .start();
                        } else f.default.instance.delFoodNode(e.node), (e.node = null);
                    }
                    e.isRecovery && f.default.instance.gameModelInstance._layer_food.addFood(e.foodType);
                }),
                (t.prototype.refreshShieldPos = function () {
                    if (this.is_show_snake_shield && this.snake_shield) {
                        var e,
                            t,
                            o,
                            n = (this.snake_size_left + this.snake_size_right) / 2,
                            i = (this.snake_size_top + this.snake_size_bottom) / 2;
                        (o =
                            (e =
                                (this.snake_size_left < 0 && this.snake_size_right < 0) ||
                                (this.snake_size_left > 0 && this.snake_size_right > 0)
                                    ? Math.abs(Math.abs(this.snake_size_left) - Math.abs(this.snake_size_right))
                                    : Math.abs(Math.abs(this.snake_size_left) + Math.abs(this.snake_size_right))) >
                            (t =
                                (this.snake_size_bottom < 0 && this.snake_size_top < 0) ||
                                (this.snake_size_bottom > 0 && this.snake_size_top > 0)
                                    ? Math.abs(Math.abs(this.snake_size_bottom) - Math.abs(this.snake_size_top))
                                    : Math.abs(Math.abs(this.snake_size_bottom) + Math.abs(this.snake_size_top)))
                                ? e
                                : t),
                            (o += 600),
                            (this.snake_shield.scale = o / this.snake_shield.width),
                            (this.snake_shield.active = !0),
                            (this.snake_shield.position = cc.v3(n, i, 0));
                    }
                }),
                (t.prototype.refreshShield = function () {
                    var e = !0;
                    this.snakeControl.isIgnore || this.snakeControl._isShield || (e = !1),
                        e
                            ? this.snake_shield ||
                              ((this.snake_shield = game.UIManager.instance.createNodeByPreloading(
                                  d.default.Game,
                                  l.default.GameBundle.prefabs.assetsList.snake_shield
                              )),
                              (this.snake_shield.parent = this.node),
                              (this.snake_shield.active = !1))
                            : this.snake_shield && (this.snake_shield.active = !1),
                        (this.is_show_snake_shield = e);
                }),
                (t.prototype.showMissileSize = function (e) {
                    this.snake_head &&
                        (this.snake_head.getChildByName("image").getChildByName("AttackRange").active = e);
                }),
                (t.prototype.show_blood = function (e, t) {
                    if ((void 0 === t && (t = !1), this.snake_head)) {
                        var o = f.default.instance.getBloodNode();
                        (o.parent = this.snake_head.getChildByName("stars")),
                            (o.getComponent(y.default).isAddBlood = e),
                            o.getComponent(y.default).playAni(t);
                    }
                }),
                (t.prototype.showAddSpeedAni = function () {
                    if (1 != this.snakeControl.now_speed) {
                        this._addSpeedDance++, 10 == this._addSpeedDance && (this._addSpeedDance = 0);
                        for (
                            var e = [],
                                t = f.default.instance.gameModelInstance._gameCameraPos,
                                o = h.default.instance.size,
                                n = 0;
                            n < this.snakeControl.snake_body.length;
                            n++
                        )
                            if (n % 10 == this._addSpeedDance) {
                                var i = this.snakeControl.snake_body[n];
                                if (
                                    i.x >= t.x - o.width / 2 &&
                                    i.x <= t.x + o.width / 2 &&
                                    i.y >= t.y - o.height / 2 &&
                                    i.y <= t.y + o.height / 2
                                ) {
                                    if (e.length > 1 && i.x == e[e.length - 1].x && i.y == e[e.length - 1].y) continue;
                                    e.push(i);
                                }
                            }
                        if (e.length > this._addSpeedNode.length)
                            for (n = this._addSpeedNode.length; n < e.length; n++) {
                                var a = f.default.instance.getSpeedNode();
                                this._addSpeedNode.push(a);
                            }
                        if (e.length < this._addSpeedNode.length)
                            for (n = e.length; n < this._addSpeedNode.length; n++)
                                (a = this._addSpeedNode.pop()), f.default.instance.delSpeedNode(a);
                        for (s = 0; s < e.length; s++) {
                            var r = e[s];
                            ((c = this._addSpeedNode[s]).parent = this.node),
                                (c.active = !0),
                                (c.x = r.x),
                                (c.y = r.y),
                                (c.angle = r.angle + 90);
                        }
                    } else
                        for (var s = 0; s < this._addSpeedNode.length; s++) {
                            var c;
                            (c = this._addSpeedNode[s]).active = !1;
                        }
                }),
                (t.prototype.refreshDressUp = function () {
                    var e = Number(p.default.getItem("dressItemUse" + this.snakeControl.skin_index, "-1"));
                    this.snakeControl.mainplayer ||
                        (e = p.default.randomFrom(-u.default.DecorateData.length, u.default.DecorateData.length - 1));
                    var t = this.snake_head
                        .getChildByName("image")
                        .getChildByName("decorate_icon")
                        .getComponent(cc.Sprite);
                    if (e < 0) t.spriteFrame = null;
                    else {
                        var o = game.BundleManager.instance
                            .getBundle(d.default.Game)
                            .get(l.default.GameBundle.textures.assetsList["decorate_icon_" + e], cc.SpriteFrame);
                        t.spriteFrame = o;
                    }
                }),
                r([g], t)
            );
        })(cc.Component));
i.default = v;
